package no.weather.weatherProvider;

import java.security.InvalidParameterException;
import java.util.List;

import no.weather.WeatherElement;
import no.weather.WeatherStation;
import no.weather.misc.IProgress;
import no.weather.weatherProxy.wsKlima.WsKlimaProxy;
import android.content.Context;
import android.os.AsyncTask;

public class GetWeatherNowAsync extends
		AsyncTask<WeatherStation, Float, List<WeatherElement>> implements
		IProgress {
	

	private Context mContext;
	
	public GetWeatherNowAsync(Context contex) {
		super();
		mContext = contex;
	}

	@Override
	protected List<WeatherElement> doInBackground(WeatherStation... params) {
		WeatherStation station = params[0];
		Context contex = mContext;
		WeatherProvider provider = new WeatherProvider(contex);
		if (!station.getTypesIsKnown()) {
			try {
				station.setTypes(provider.getWeatherTypes(station.getId(),
						station.getProvider(), false));
			} catch (StationIsDeletedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		WsKlimaProxy wsklima = new WsKlimaProxy(contex);
		if (station.getProvider().equals(WsKlimaProxy.PROVIDER))
			return wsklima.getWeatherNow(station, this, null);
		else
			throw new InvalidParameterException(station.getProvider()
					+ " is not provided in getWeatherNow");
	}

	@Override
	public void publishProgress(Float progress) {
		publishProgress(progress);
	}
}